package com.example.demo;

import cn.hutool.core.lang.Dict;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import com.google.common.base.CaseFormat;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
public class JpaGenerator {

    private static final String JAVA_SRC = "src/main/java";

    private static final String SLASH = "/";

    private static final String JAVA_SUFFIX = ".java";

    public static void main(String[] args) throws IOException {
        String packageName = "com.example.demo";// 包名
        String entityName = "Demo";// 实体类
        String tablePrefix = "t_";// 表前缀
        createFiles(packageName, entityName, tablePrefix);
        log.info("jpa逆向工程创建成功！");
    }

    private static void createFiles(String packageName, String entityName, String tablePrefix) throws IOException {
        String tableName = tablePrefix + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, entityName);
        String entityNameLower = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, entityName);
        String routePath = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, entityName);
        String packagePath = packageName.replaceAll("\\.", SLASH);
        String entity = "entity";
        String dao = "dao";
        String service = "service";
        String controller = "controller";
        String converter = "converter";
        String dto = "dto";
        String author = System.getProperty("user.name");
        String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("/templates/jpa/", TemplateConfig.ResourceMode.CLASSPATH));
        // 创建Entity
        File entityPackage = new File(JAVA_SRC + SLASH + packagePath + SLASH + entity);
        if (!entityPackage.exists()) entityPackage.mkdirs();
        FileWriter entityWriter = new FileWriter(entityPackage.getPath() + SLASH + entityName + JAVA_SUFFIX);
        Template entityTemplate = engine.getTemplate("Entity.ftl");
        entityTemplate.render(Dict.create().set("PACKAGE_NAME", packageName).set("ENTITY_NAME", entityName).set("TABLE_NAME", tableName).set("USER", author).set("DATE", date).set("TIME", time), entityWriter);
        // 创建Repository
        File repositoryPackage = new File(JAVA_SRC + SLASH + packagePath + SLASH + dao);
        if (!repositoryPackage.exists()) repositoryPackage.mkdirs();
        FileWriter repositoryWriter = new FileWriter(repositoryPackage.getPath() + SLASH + entityName + "Repository" + JAVA_SUFFIX);
        Template repositoryTemplate = engine.getTemplate("Repository.ftl");
        repositoryTemplate.render(Dict.create().set("PACKAGE_NAME", packageName).set("ENTITY_NAME", entityName).set("USER", author).set("DATE", date).set("TIME", time), repositoryWriter);
        // 创建Service
        File servicePackage = new File(JAVA_SRC + SLASH + packagePath + SLASH + service);
        if (!servicePackage.exists()) servicePackage.mkdirs();
        FileWriter serviceWriter = new FileWriter(servicePackage.getPath() + SLASH + entityName + "Service" + JAVA_SUFFIX);
        Template serviceTemplate = engine.getTemplate("Service.ftl");
        serviceTemplate.render(Dict.create().set("PACKAGE_NAME", packageName).set("ENTITY_NAME", entityName).set("ENTITY_NAME_LOWER", entityNameLower).set("USER", author).set("DATE", date).set("TIME", time), serviceWriter);
        // 创建Controller
        File controllerPackage = new File(JAVA_SRC + SLASH + packagePath + SLASH + controller);
        if (!controllerPackage.exists()) controllerPackage.mkdirs();
        FileWriter controllerWriter = new FileWriter(controllerPackage.getPath() + SLASH + entityName + "Controller" + JAVA_SUFFIX);
        Template controllerTemplate = engine.getTemplate("Controller.ftl");
        controllerTemplate.render(Dict.create().set("PACKAGE_NAME", packageName).set("ENTITY_NAME", entityName).set("ENTITY_NAME_LOWER", entityNameLower).set("ROUTE_PATH", routePath).set("USER", author).set("DATE", date).set("TIME", time), controllerWriter);
        // 创建Converter
        File converterPackage = new File(JAVA_SRC + SLASH + packagePath + SLASH + converter);
        if (!converterPackage.exists()) converterPackage.mkdirs();
        FileWriter converterWriter = new FileWriter(converterPackage.getPath() + SLASH + entityName + "Converter" + JAVA_SUFFIX);
        Template converterTemplate = engine.getTemplate("Converter.ftl");
        converterTemplate.render(Dict.create().set("PACKAGE_NAME", packageName).set("ENTITY_NAME", entityName).set("USER", author).set("DATE", date).set("TIME", time), converterWriter);
        // 创建SaveDto
        File dtoPackage = new File(JAVA_SRC + SLASH + packagePath + SLASH + dto);
        if (!dtoPackage.exists()) dtoPackage.mkdirs();
        FileWriter saveDtoWriter = new FileWriter(dtoPackage.getPath() + SLASH + entityName + "SaveDto" + JAVA_SUFFIX);
        Template saveDtoTemplate = engine.getTemplate("SaveDto.ftl");
        saveDtoTemplate.render(Dict.create().set("PACKAGE_NAME", packageName).set("ENTITY_NAME", entityName).set("USER", author).set("DATE", date).set("TIME", time), saveDtoWriter);
    }
}
